#include "mylabel.h"
#include <QDebug>
#include <QString>
#include <QPropertyAnimation>
#include <QGraphicsOpacityEffect>


QString digitBkg[30] = {
    "rgb(192,192,192)",
    "rgb(255,165,0)",
    "rgb(85,107,47)",
    "rgb(143,188,143)",
    "rgb(0,255,255)",
    "rgb(138,43,226)",
    "rgb(216,191,216)",
    "rgb(188,143,143)",
    "rgb(240,248,255)",
    "rgb(102,205,170)",
    "rgb(25,25,112)",
    "rgb(184,134,11)",
    "rgb(178,34,34)",
    "rgb(221,160,221)",
    "rgb(127,255,212)",
    "rgb(30,144,255)",
    "rgb(210,105,30)",
    "rgb(255,228,225)",
    "rgb(245,255,250)",
    "rgb(248,248,255)",
    "rgb(205,133,63)",
    "rgb(255,192,203)",
    "rgb(218,112,214)",
    "rgb(75,0,130)",
    "rgb(95,158,160)",
    "rgb(34,139,34)",
    "rgb(189,183,107)",
    "rgb(205,92,92)",
    "rgb(0,0,128)",
    "rgb(0,255,0)"
};


int log_2(int value)
{
    if(value == 1){
        return 0;
    }else{
        return 1+log_2(value>>1);
    }
}

MyLabel::MyLabel(int text)
{
    this->setText(QString::number(text));
    this->setAlignment(Qt::Alignment(Qt::AlignCenter));
    this->setFont(QFont("Gadugi", 20, QFont::Bold));

    //初始化样式
    int index = log_2(text) - 1;
    QString fontColor = "color:rgb(255,255,255);";
    if(index < 8)
    {
        fontColor = "color:rgb(119,110,101);";
    }
    QString bgColor = QString("QLabel{background-color:%1;border-radius:5px;%2}").arg(digitBkg[index]).arg(fontColor);
    this->setStyleSheet(bgColor);


    QGraphicsOpacityEffect *m_pGraphicsOpacityEffect = new QGraphicsOpacityEffect(this);
    m_pGraphicsOpacityEffect->setOpacity(1);
    this->setGraphicsEffect(m_pGraphicsOpacityEffect);


    QPropertyAnimation *animation = new QPropertyAnimation(m_pGraphicsOpacityEffect,"opacity",this);
    animation->setEasingCurve(QEasingCurve::Linear);
    animation->setDuration(400);
    animation->setStartValue(0);
    animation->setEndValue(1);
    animation->start(QAbstractAnimation::KeepWhenStopped);
}

void MyLabel::reSetText(int text)
{
    this->setText(QString::number(text));
    int index = log_2(text) - 1;
    QString fontColor = "color:rgb(255,255,255);";
    if(index < 8)
    {
        fontColor = "color:rgb(119,110,101);";
    }
    QString bgColor = QString("QLabel{background-color:%1;border-radius:5px;%2}").arg(digitBkg[index]).arg(fontColor);
    this->setStyleSheet(bgColor);
    this->show();
    this->repaint();
}
